added Feb 2001 SDK
[windows-sources.git] / shared source / sscli20 / jscript / engine / package.cs
blob7d0ab7be59342d3c70b355148b9af8fd7ab021f5
1 // ==++==
2 //
3 //
4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
5 //
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
10 //
11 // You must not remove this notice, or any other, from this software.
12 //
13 //
14 // ==--==
16 namespace Microsoft.JScript {
18 using Microsoft.JScript.Vsa;
19 using System;
20 using System.Reflection;
21 using System.Reflection.Emit;
23 public sealed class Package : AST{
24 private String name;
25 private ASTList classList;
26 private PackageScope scope;
28 internal Package(String name, AST id, ASTList classList, Context context)
29 : base(context){
30 this.name = name;
31 this.classList = classList;
32 this.scope = (PackageScope)Globals.ScopeStack.Peek();
33 this.scope.owner = this;
34 this.Engine.AddPackage(this.scope);
35 Lookup simpleId = id as Lookup;
36 if (simpleId != null)
37 simpleId.EvaluateAsWrappedNamespace(true);
38 else{
39 Member qualifiedId = id as Member;
40 if (qualifiedId != null)
41 qualifiedId.EvaluateAsWrappedNamespace(true);
45 internal override Object Evaluate(){
46 Globals.ScopeStack.Push(this.scope);
47 try{
48 for (int i = 0, n = this.classList.count; i < n; i++)
49 this.classList[i].Evaluate();
50 return new Completion();
51 }finally{
52 Globals.ScopeStack.Pop();
56 public static void JScriptPackage(String rootName, VsaEngine engine){
57 GlobalScope scope = ((IActivationObject)engine.ScriptObjectStackTop()).GetGlobalScope();
58 FieldInfo field = scope.GetLocalField(rootName);
59 if (field == null)
60 field = scope.AddNewField(rootName, Namespace.GetNamespace(rootName, engine), FieldAttributes.Public|FieldAttributes.Literal);
63 internal void MergeWith(Package p){
64 for (int i = 0, n = p.classList.count; i < n; i++)
65 this.classList.Append(p.classList[i]);
66 this.scope.MergeWith(p.scope);
69 internal override AST PartiallyEvaluate(){
70 this.scope.AddOwnName();
71 Globals.ScopeStack.Push(this.scope);
72 try{
73 for (int i = 0, n = this.classList.count; i < n; i++)
74 this.classList[i].PartiallyEvaluate();
75 return this;
76 }finally{
77 Globals.ScopeStack.Pop();
81 internal override void TranslateToIL(ILGenerator il, Type rtype){
82 Globals.ScopeStack.Push(this.scope);
83 for (int i = 0, n = this.classList.count; i < n; i++)
84 this.classList[i].TranslateToIL(il, Typeob.Void);
85 Globals.ScopeStack.Pop();
88 internal override void TranslateToILInitializer(ILGenerator il){
89 String root = this.name;
90 int j = root.IndexOf('.');
91 if (j > 0)
92 root = root.Substring(0, j);
93 il.Emit(OpCodes.Ldstr, root);
94 this.EmitILToLoadEngine(il);
95 il.Emit(OpCodes.Call, CompilerGlobals.jScriptPackageMethod);
96 Globals.ScopeStack.Push(this.scope);
97 for (int i = 0, n = this.classList.count; i < n; i++)
98 this.classList[i].TranslateToILInitializer(il);
99 Globals.ScopeStack.Pop();
102 internal override Context GetFirstExecutableContext(){
103 return null;